<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Framework_User
 *
 * @author      Joe Stump <joe@joestump.net>
 * @copyright   (c) 2006 Joseph C. Stump. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package Framework
 * @filesource
 */

/**
 * Framework_User
 *
 * This is the default user class. The default behavior is to take the $userID
 * passed to the constructor and fetch the record based on the various
 * FRAMEWORK_USER_* defines (see above). The PHP5 overloading is then used to
 * fetch and set data to the user class. NOTE: this class does NOT interface
 * with the database other than to select basic information.
 *
 * @author Joe Stump <joe@joestump.net>
 * @package Framework
 */
class Framework_User 
{
    /**
     * $data
     *
     * @access public
     * @var array $data Record from users table
     */
    protected $data = array();

    /**
     * __construct
     *
     * @author Joe Stump <joe@joestump.net>
     * @access public
     * @return void
     */
    public function __construct($userID = null)
    {
        //parent::__construct();
        $userConfig = Framework::$site->config('Framework', 'User');
        if (is_null($userID)) {
        	
        	$userField = $userConfig['userField'];
            $session = Framework_Session::singleton(); 
            // Look for ID in the session
            if (!$session->$userField) {
            	// If it isn't in the session, get anon Id (defaultUser)
                //$userID = Framework::$site->config->user->defaultUser;
                $this->data = Framework::$site->config('Framework', 'DefaultUser');
                $session->$userField = $userConfig['userField'];
                $userID = -1;
            } else {
            	// Otherwise use id from the session
                $userID = $session->$userField;
            }
            
            
        } 
        
     	// subclasses can throw up a -1 if they dont want the following
        // behaviour to happen
		if ($userID != -1) {
	       
	        $table = Framework_DB::tableFactory($userConfig['userTable']);
	        
	        $queryArray = array(
	        	'sqlKey' => array(
	        		'select' => '*',
	        		'where'  => $userConfig['userField'].' = "'.$userID.'"',
	        		'get'    => 'row'
	        	)
	        );
	        
	        
	
	        $result = $table->select($queryArray);
	        
	        if (!PEAR::isError($result) && count($result)) {
	            $this->data = $result;
	        } else {
	        	throw new Framework_Exception('Could not look up userID');
	        	
	        }
		}

        
    }

    /**
     * toArray - Overriding base toArray to make it useful
     *
     * Traverses self::data and returns that information
     *
     * @return  array
     */
    function toArray()
    {
        $aArray = Array();
        
        foreach($this->data as $k=>$v) {
            if ($v != null)
            	$aArray[$k] = $v;
        }
        
        return $aArray;
    }
    
    /**
     * __get
     *
     * @access public
     * @param string $var Name of var 
     * @return mixed Null if the var does not exist, var's value if it does 
     */
    public function __get($var)
    {
    	
        if (isset($this->data[$var])) {
            return $this->data[$var];
        }
       
        return null;
    }

    /**
     * __set
     *
     * @access public
     * @param string $var Name of var to set
     * @param mixed $val Value of given var
     * @return void
     * @see Framework_User::$data
     */
    public function __set($var,$val) 
    {
        $this->data[$var] = $val;
    }

    /**
     * __isset  
     * 
     * @access public
     * @param string $var Name of var to check if it's in $data
     * @return boolean
     * @see Framework_User::$data
     */
    //public function __isset($var)
    //{
    //    return isset($this->data[$var]);
    //}

    /**
     * isDefault
     *
     * @access public
     * @return boolean
     */
    public function isDefault()
    {
    	$userConfig = Framework::$site->config('Framework', 'User');
        return ($this->{$userConfig['userField']} == $userConfig['defaultUser']);
    }

    /**
     * singleton
     *
     * @access public
     * @return mixed PEAR_Error on failure, user class on success
     * @static
     */
    static public function singleton()
    {
        static $user = null;
        $userConfig = Framework::$site->config('Framework', 'User');
        if (is_null($user)) {
            if (!isset($userConfig['userClass'])) {
                $file = null;
                $class = 'Framework_User';
            } else {
                $file = 'Framework/User/'.$userConfig['userClass'].'.php';
                $class = 'Framework_User_'.$userConfig['userClass'];
            }
            
            if (!is_null($file)) {
                if (!include_once($file)) {
                    return PEAR::raiseError('Could not load class file: '.$file);
                }
            }

            if (class_exists($class)) {
                $user = new $class();
            } else {
                return PEAR::raiseError('Unable to load class: '.$userConfig['userClass']);
            }
        }

        return $user;
    }
}

?>
